package com.itheima.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class JwtUtils {
    private static  String signKey = "gaofeng";
    private static  Integer expire = 1000 * 60 * 30;

    public static String gernateJWT(Map<String,Object> claims){
        String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, signKey)
                .setClaims(claims).setExpiration(new Date(System.currentTimeMillis() + expire)).compact();//自定义内容
        log.info("jwt:{}", jwt);
        return jwt;
    }

    public static Claims parseJWT(String jwt) {
        Claims claims = Jwts.parser().setSigningKey(signKey)
                .parseClaimsJws(jwt).getBody();
        log.info("id:{}", claims.get("id"));
        log.info("username:{}", claims.get("username"));
        log.info("expiration:{}", claims.getExpiration());
        return claims;
    }
}
